package com.zprmb.udzt;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.zprmb.udzt.layout.AutoLayoutActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 手势Activity
 *
 * @author shenshao
 */
public class GeActivity extends AutoLayoutActivity {

    private SharedPreferences sp;
    private TelephonyManager tm;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        sp = getSharedPreferences("config", MODE_PRIVATE);
        tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
    }

    protected void onStop() {
        super.onStop();
        if (isTopActivity("com.zprmb.udzt")) {
            Log.e("Ge", "在前台");
        } else {
            //Toast.makeText(this, "不在前台了1111", 0).show();
            Editor ed = sp.edit();
            ed.putString("time", ondata());
            ed.putInt("isopen", 1);
            ed.commit();

            Editor editor = sp.edit();
            editor.putString("isTop", "1");
            editor.commit();
            Log.e("退出运行", "1");
        }
    }

    public boolean isFinger() {
        if (getisFinger() == 1 && getopenfinger() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public int getisFinger() {
        return sp.getInt("finger", 0);
    }

    public int getopenfinger() {
        return sp.getInt(getMobile() + "openfinger", 0);
    }

    public String getMobile() {
        return sp.getString("mobile", "");
    }

    public String getSex() {
        return sp.getString("sex", "女");
    }

    public String getdevice_token() {
        return sp.getString("device_token", "");
    }

    public String getdeviceId() {
        return tm.getDeviceId();

    }

    public String ondata() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public boolean isTopActivity(String packageName) {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
        if (tasksInfo.size() > 0) {
            // System.out.println("---------------包名-----------"+tasksInfo.get(0).topActivity.getPackageName());
            // 应用程序位于堆栈的顶层
            if (packageName.equals(tasksInfo.get(0).topActivity
                    .getPackageName())) {
                return true;
            }
        }
        return false;
    }

    public String getAppVersion() {
        // 获取手机的包管理者
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packInfo = pm.getPackageInfo(getPackageName(), 0);
            return packInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            // 不可能发生.
            return "获取版本号失败";
        }
    }

    public String getDeviceType() {
        Build build = new Build();
        return build.MODEL;
    }

    public boolean network() {
        boolean flag = false;

        //得到网络连接信息
        ConnectivityManager manager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        //去进行判断网络是否连接
        if (manager.getActiveNetworkInfo() != null) {
            flag = manager.getActiveNetworkInfo().isAvailable();
        }

        return flag;
    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}
